<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Payroll extends User_Controller
{
	private $response = array();
	private $userModel;
	private $clientModel;
	private $requestManager;
	private $settings;
    public function __construct()
    {
        parent::__construct();
        
    	if(isset($this->session) && !$this->session->userdata('isLogged')) {
        	redirect('/');
        }
        
        $this->userModel = $this->load->model('User_Model');
        $this->clientModel = $this->load->model('Client_Model');
        $this->load->helper('RequestManager');
        $this->requestManager = RequestManager::getInstance();
        $this->settings = $this->config->item('settings');
        $this->load->helper(array('ConstantsUtils','StringUtils','DateUtils'));
    
        
    }

    public function index($payroll_period_id = 0){
    	$data = array();
    	if(is_numeric($payroll_period_id) && $payroll_period_id > 0) {
    		//$payroll_id = $this->input->post('period');
			$this->requestManager->setData(array('payroll_period_id'=>$payroll_period_id));
			$this->requestManager->setSendType("rest");
			$this->requestManager->setAccessPath($this->settings['api_path']."payroll/get_payroll/" .$this->settings['client_key']);
			$return = $this->requestManager->send();
			if($return->message == 'success'){
				$data['message'] = $return->message;
				$data['timesheet'] = $return->data;
				$data['pending'] = $return->pending;
				$data['confirmed'] = $return->confirmed;
				$data['submitted'] = $return->submitted;
			}
    	}
		// get all payroll periods
		$this->requestManager->setData(array());
		$this->requestManager->setSendType("rest");
		$this->requestManager->setAccessPath($this->settings['api_path']."payroll/client_payroll_periods/" .$this->settings['client_key']);
		$return = $this->requestManager->send();
		if($return->message == 'success'){
			$data['periods'] = $return->data;
			
		
		}
		
    	$data['payroll_period_id'] = $payroll_period_id;
    	$data['message'] = "Payroll Page";
    	$this->load->view('header');
		$this->load->view('index',$data);
		$this->load->view('footer');
    }
    
	public function view_user($payroll_period_id, $user_id){
		$this->load->helper(array('ConstantsUtils','StringUtils','DateUtils'));
		$data = array();
		$this->requestManager->setData(array('payroll_period_id'=>$payroll_period_id, 'user_id'=>$user_id));
		$this->requestManager->setSendType("rest");
		$this->requestManager->setAccessPath($this->settings['api_path']."payroll/view_user/" .$this->settings['client_key']);
		$return = $this->requestManager->send();
		//var_dump($return);
		
		if($return->message == 'success'){
			
			if(!isset($return->user_payroll)){
				$data['message'] = $return->message;
				$data['timesheet'] = $return->data;
				$data['special_worked_holiday'] = $return->special_holiday;
				$data['regular_worked_holiday'] = $return->regular_holiday;
				$data['rest_day_info'] = $return->rest_day;
			}else{
				$data['user_payroll'] = $return->user_payroll;
			}

			$data['member'] = $return->member;
			$data['period'] = $return->period;
			$data['contributions'] = $return->contributions;
			$data['label'] = array('Regular Hours',
			'Regular Overtime', 'Reg. Night Diff',
			'Restday','Excess on Restday','Restday Night Diff',
			'Special Holiday','Excess on Special Holiday','Special Holiday Night Diff',
			'Special Holiday on Restday','Excess Special Holiday on Restday',
			'Legal Holiday','Excess on Legal Holiday','Legal Holiday Unwork',
			'Legal Holiday on Restday','Excess Legal Holiday on Restday',
			'Legal Holiday Night Diff','Adjustment/CA','Incentives/Maternity/Sickness Reimbursement',
			'Undertime','Late');
			
			$data['fields'] = array('regular_hours',
			'regular_overtime','regular_night_differential','restday','excess_restday','restday_night_differential',
			'special_holiday','excess_special_holiday','special_holiday_night_differential',
			'special_holiday_restday','excess_special_holiday_restday','legal_holiday',
			'excess_legal_holiday','legal_holiday_unwork','legal_holiday_restday','excess_legal_holiday_restday',
			'legal_holiday_night_differential','adjustment','incentives','approved_undertime','approved_late');
		
			$this->requestManager->setData(array('user_id'=>$user_id));
			$this->requestManager->setSendType("rest");
			$this->requestManager->setAccessPath($this->settings['api_path']."user/getAllRates/" .$this->settings['client_key']);
			$return = $this->requestManager->send();
			if($return->message == 'success'){
				$rates = array();
				foreach ($return->data[0] as $rate){
					$rates[] = $rate;
				}
			}
			$data['rates'] = $rates;
		}
		
		$this->load->view('header');
		$this->load->view('view_user',$data);
		$this->load->view('footer');
	}
    
	public function get_payroll($payroll_period_id){
		$data = array();
		$url = $this->settings['api_path'] ."payroll/get_payroll/" . $this->settings['client_key']."/$payroll_id";
		$json = file_get_contents($url);
		$array = json_decode($json);
		$data['message'] = $array->message;
		$data['payroll'] = $array->data;
		$this->load->view('payroll',$data);
	}
	
	public function update_field($payroll_period_id,$user_id,$pid){
		$data = array();
		$this->requestManager->setData(array('payroll_period_id'=>$payroll_period_id, 'user_id'=>$user_id, 'field_id'=>$_POST['id'],'field_value'=>$_POST['value'],'pid'=>$pid));
		$this->requestManager->setSendType("post");
		$this->requestManager->setAccessPath($this->settings['api_path']."payroll/update_field/" .$this->settings['client_key']);
		$return = $this->requestManager->send();
		//var_dump($return);
		//var_dump(array('payroll_period_id'=>$payroll_period_id, 'user_id'=>$user_id, 'field_id'=>$_POST['id'],'field_value'=>$_POST['value'],'pid'=>$pid));
		if($return->message == 'success'){
			die("{$_POST['value']}");
		}
	}
	
	public function update_data(){
		$data = array();
		if($this->input->post()){
			$this->requestManager->setData($this->input->post());
			$this->requestManager->setSendType("post");
			$this->requestManager->setAccessPath($this->settings['api_path']."payroll/save_payroll_amount/" .$this->settings['client_key']);
			$return = $this->requestManager->send();
			//var_dump($return);
			if($return->message == 'success'){
				$this->session->set_userdata('message', 'Payroll data successfully updated!');
				redirect('payroll/index/'.$this->input->post('payroll_period_id'));
			}
		}
		//var_dump($this->input->post());
		//echo $this->input->post('pid');
	}
	
	
}